package cn.lotusnut.rabbitmqboot.delay;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author JiangMing Zhou
 * @Date 2022/12/1 22:07
 * @Version 1.8
 */
@Component
public class DelayProvider {
    @Resource
    private AmqpTemplate amqpTemplate;

    public void sendMessage(String message, int delayTime) {
        System.out.println("发送延迟消息：" + new Date());
        amqpTemplate.convertAndSend("delay_exchange", "delay", message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(delayTime);
                return message;
            }
        });
    }
}
